<?php
/**
 * Object Relational Persistence Mapping Class for table: pha_ewh_imticket_detail
 *
 * @package	modules.dept_pharmacy.ordo
 * @author	viethd
 * @version $
 * @copyright 2009 LHV Company
 */
class PhaEwhImticketDetail extends ORDataObject {
    const VALUATED = 1;

    /**#@+
     * Fields of table: pha_ewh_imticket_detail mapped to class members
     */
    var $pha_ewh_imticket_detail_id		= '';
    var $pha_ewh_imticket_id		= '';
    var $pha_drug_material_id		= '';
    var $im_unit_price		= '';
    var $im_lot		= '';
    var $im_expired_date		= '';
    var $im_quantity		= '';
    var $remain_quantity		= '';
    var $latest_inventory_quantity		= '';
    var $insurance_price		= '';
    var $service_price		= '';
    var $allow_sell		= '';
    var $valuated		= '';
    var $run_out		= '';
    /**#@-*/


    /**
     * DB Table
     */
    var $_table = 'pha_ewh_imticket_detail';

    /**
     * Primary Key
     */
    var $_key = 'pha_ewh_imticket_detail_id';

    /**
     * Internal Name
     */
    var $_internalName = 'PhaEwhImticketDetail';

    /**
     * Handle instantiation
     */
    function __construct() {
        parent::ORDataObject();
    }

    public function setData($data = array()) {
        if (is_array($data)) {
            $this->populateArray($data);
        }
    }

    public function delete($imTicketId, $imticketDetailId) {
         
        $sql = "DELETE FROM $this->_table WHERE pha_ewh_imticket_id='$imTicketId'
        AND pha_ewh_imticket_detail_id='$imticketDetailId' " ;
        $this->_execute($sql);
    }

    public function isExist($imTicketId, $drugMaterialId) {
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "pha_ewh_imticket_detail_id",
            'from'  => "$this->_table",
            'where' => "pha_ewh_imticket_id='$imTicketId' AND pha_drug_material_id='$drugMaterialId'"
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
            return false;
        }
        return true;
    }

    public function saveValuatedPrice($imticketDetailId,
    $insurancePrice, $servicePrice,
    $allowSell, $valuated) {

        $sql = "UPDATE $this->_table
        SET 
        insurance_price=$insurancePrice,
        service_price =$servicePrice,
        allow_sell =$allowSell,
        valuated =$valuated
        WHERE pha_ewh_imticket_detail_id = '$imticketDetailId'";
        $this->_execute($sql);

    }

    public function getLastedImportedItem() {
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'      => "pha_ewh_imticket_detail_id, im_unit_price",
            'from'      => "$this->_table t INNER JOIN pha_ewh_imticket pei 
                            ON (t.pha_drug_material_id='" . $this->pha_drug_material_id . "' AND t.pha_ewh_imticket_id=pei.pha_ewh_imticket_id)",
            'orderby'   =>  "imticket_code desc",
            'limit'     => 1

        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
            return null;
        }
        return $result[0];
    }

    public function pickDrugMaterial($pickedQty) {
        $rqty = $this->remain_quantity - $pickedQty;
        if ($qty < 0) {
            //TODO throw exception
        } else if ($rqty == 0) {
            $sql = "UPDATE $this->_table SET remain_quantity = '$rqty', run_out=1 WHERE pha_ewh_imticket_detail_id='" . $this->pha_ewh_imticket_detail_id . "' AND allow_sell=1" ;
            $this->_execute($sql);
            
        } else {
            $sql = "UPDATE $this->_table SET remain_quantity = '$rqty' WHERE pha_ewh_imticket_detail_id='" . $this->pha_ewh_imticket_detail_id . "' AND allow_sell=1" ;
            $this->_execute($sql);            
        }       
    }

    public function listAddedDrugMaterial($imTicketId) {
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "pha_ewh_imticket_detail_id, t.pha_drug_material_id, proprietary_name,
            im_lot,im_quantity, enum_unit_import_sell, im_expired_date, im_unit_price, (im_quantity*im_unit_price) total_price",
            'from'  => "$this->_table t INNER JOIN pha_drug_material pdm 
            ON(t.pha_ewh_imticket_id='$imTicketId' AND t.pha_drug_material_id=pdm.pha_drug_material_id)"            
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        return $result;
    }

    public function listAllowSellDMOfGivenNameId($drugMaterialId) {
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "pha_ewh_imticket_detail_id, proprietary_name,
                        im_lot,im_quantity, enum_unit_import_sell, im_expired_date, remain_quantity,
                        im_unit_price, imticket_code, pha_provider_id",
            'from'  => "$this->_table t INNER JOIN pha_ewh_imticket pei ON (t.pha_ewh_imticket_id=pei.pha_ewh_imticket_id 
                                                                        AND t.pha_drug_material_id='$drugMaterialId' 
                                                                        AND allow_sell=1
                                                                        AND remain_quantity>0)
                                        INNER JOIN pha_drug_material pdm ON(t.pha_drug_material_id=pdm.pha_drug_material_id)"            
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        return $result;
    }

    public function listDrugMaterialWithValPrice($imTicketId) {
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "t.pha_ewh_imticket_detail_id, t.pha_ewh_imticket_id, t.pha_drug_material_id, proprietary_name, allow_sell,
                    im_lot,im_quantity, remain_quantity, enum_unit_import_sell,
                    im_expired_date, im_unit_price, (im_quantity*im_unit_price) total_price,
                    service_rate, insurance_price, service_price, insurance_rate, enum_rate_type, valuated",
            'from'  => "$this->_table t INNER JOIN pha_drug_material pdm 
                    ON(t.pha_ewh_imticket_id='$imTicketId' AND t.pha_drug_material_id=pdm.pha_drug_material_id )
                    LEFT JOIN pha_ewh_valuate pev ON (t.pha_ewh_imticket_detail_id=pev.pha_ewh_imticket_detail_id)"            
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        return $result;
    }


    public function allowSell($imTicketId, $imTicketDetailId, $allowSell) {
        $sql = "UPDATE $this->_table SET allow_sell = '$allowSell' WHERE pha_ewh_imticket_id='$imTicketId' AND pha_ewh_imticket_detail_id='$imTicketDetailId' AND valuated=1" ;
        $this->_execute($sql);

    }

}
?>
